﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StudentInfo.Areas.StudentArea.ViewModelHelpers;
using StudentInfo.Areas.StudentArea.Services;
using StudentInfo.Areas.StudentArea.ViewModels;
using StudentInfo.Areas.StudentArea.DAL;

namespace StudentInfo.Areas.StudentArea.Controllers
{
    public class ExamController : Controller
    {
        //
        // GET: /StudentArea/Exam/

        public ActionResult Index()
        {
            var exams = ExamViewModelHelper.PopulateExamViewModelList(ExamService.GetAll());
            return View(exams);
        }

        public ActionResult Details(int examId)
        {
            var exam = ExamService.Get(examId);

            if (exam != null)
                return View(ExamViewModelHelper.PopulateExamViewModel(exam));

            return RedirectToRoute("Exam", new { action = "Index" });
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ExamViewModel model)
        {
            var exam = ExamViewModelHelper.PopulateExamFromViewModel(model);

            if (exam != null)
                //ExamService.Create(exam);
                using (var repository = new Repository<Exam>())
                {
                    repository.Create(exam);
                }


            return RedirectToRoute("Exam", new { action = "Index" });
        }

        public ActionResult Edit(int examId)
        {
            var exam = ExamService.Get(examId);

            if (exam != null)
                return View(ExamViewModelHelper.PopulateExamViewModel(exam));

            return RedirectToRoute("Exam", new { action = "Index" });
        }

        [HttpPost]
        public ActionResult Edit(ExamViewModel model)
        {
            var exam = ExamViewModelHelper.PopulateExamFromViewModel(model);

            if (exam != null)
             //   ExamService.Save(exam);
                using (var repository = new Repository<Exam>())
                {
                    repository.Update(exam);
                }

            return RedirectToRoute("Exam", new { action = "Details", examId = exam.Id });
        }

        public ActionResult Delete(int examId)
        {
            var exam = ExamService.Get(examId);

            if (exam != null)
                return View(ExamViewModelHelper.PopulateExamViewModel(exam));

            return RedirectToRoute("Exam", new { action = "Index" });
        }

        [HttpPost]
        public RedirectToRouteResult Delete(int examId, FormCollection collection)
        {
            var exam = ExamService.Get(examId);

            if (exam != null)
            //    ExamService.Delete(exam);
                using (var repository = new Repository<Exam>())
                {
                    repository.Delete(exam);
                }

            return RedirectToRoute("Exam", new { action = "Index" });
        }
    }
}
